// 適当なタイムスタンプ付きringubufferクラス
template<typename T, typename V>
    class TimeStampedRingbuffer
{
public:
    TimeStampedRingbuffer()
        :m_max_size(0), m_size(0), m_current(0){}
    
    TimeStampedRingbuffer(unsigned int max_size)
        :m_max_size(max_size), m_size(0), m_current(0){
        m_buffer.resize(m_max_size);
        m_time.resize(m_max_size);
    }
    void push(T time, V &val)
    {
        m_buffer[m_current] = val;
        m_time[m_current] = time;
        m_current ++;
        if (m_size < m_max_size) m_size ++;
        if (m_current >= m_max_size) m_current = 0;
    }
    V &operator[](unsigned int pos)
    {
        if (pos >= m_size) pos = m_size - 1; // はみ出ないように調整
        if (pos < m_current)  return m_buffer[m_current - pos - 1];
        return m_buffer[m_max_size + m_current - pos - 1];
    }
    T time(unsigned int pos)
    {
        if (pos >= m_size) pos = m_size; // はみ出ないように調整
        if (pos < m_current)  return m_time[m_current - pos - 1];
        return m_time[m_max_size + m_current - pos - 1];
    }
    unsigned int size() {return m_size;}
    void resize(unsigned int max_size)
    {
        m_max_size = max_size;
        m_buffer.resize(max_size);
        m_time.resize(max_size);
        m_current = 0;
        m_size = 0;
    }
private:
    unsigned int m_max_size, m_size, m_current;
    std::vector<V> m_buffer;
    std::vector<T> m_time;
};
